package com.wut.clock.controller;

import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.mongo.dto.HistoryParams;
import com.wut.clock.service.IHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 浏览历史管理
 * @date 2022/6/11 17:20
 */
@RequiredArgsConstructor
@RequestMapping("/history")
@RestController
@Api(value="浏览历史管理器",tags = {"浏览历史管理器"})
public class HistoryController {

    private final IHistoryService historyService;

    @ApiOperation("生成浏览历史")
    @PostMapping("addHistory")
    public CommonResult<String> addHistory(@RequestBody HistoryParams historyParams){
        if(historyService.addHistory(historyParams)>0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("删除浏览历史")
    @PostMapping("deleteHistory")
    public CommonResult<String> deleteHistory(@RequestParam("ids") List<String> ids){
        if(historyService.deleteHistory(ids)>0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("清空浏览记录")
    @PostMapping("clear")
    @ResponseBody
    public CommonResult<String> clear() {
        historyService.clear();
        return CommonResult.success();
    }

    @ApiOperation("分页获取浏览记录")
    @GetMapping("getHistoryList")
    public CommonResult<CommonPage<HistoryParams>> getHistoryList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(historyService.getHistoryList(pageNum, pageSize)));
    }
}
